package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.infrastructure.bean.GoodsDealerBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsDealerMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsDealerDto;
import com.molichuxing.services.infrastructure.dto.response.GoodsSubscribeMileageDto;
import com.molichuxing.services.infrastructure.service.GoodsDealerService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分销表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsProductDealerService")
public class GoodsDealerServiceImpl implements GoodsDealerService {

    private static final Logger log = Logger.getLogger(GoodsServiceImpl.class);

    @Resource
    private GoodsDealerMapper goodsDealerMapper;

    @Override
    public Boolean deleteGoodsDealer(int goodsId) {
        int result = goodsDealerMapper.delete(goodsId);
        if(result >= 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteGoodsDealerByDealer(int dealerId) {
        int result = goodsDealerMapper.deleteByDealerId(dealerId);
        if(result >= 0) {
            return true;
        }
        return false;
    }

    @Override
    public int getDealerCountByGoodsId(int goodsId) {
        return goodsDealerMapper.getDealerCountByGoodsId(goodsId);
    }

    @Override
    public int getGoodsCount(List<Integer> goodsIds, Integer dealerId) {
        return goodsDealerMapper.getGoodsCount(goodsIds, dealerId);
    }

    @Override
    public Map<Integer, List<GoodsDealerDto>> getGoodsCountByDealerId(List<Integer> dealerIds) {
        List<GoodsDealerBean> pageResult = goodsDealerMapper.getDealerList(null, dealerIds, null);
        if(null == pageResult || pageResult.size() == 0) {
            return null;
        }
        return Convert.toGoodsProductDealerBeanDto(pageResult).stream().collect(Collectors.groupingBy(GoodsDealerDto::getDealerId));
    }


}
